<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品筛选器
 * 
 * @package 	htdocs
 * @subpackage  quanliadmin
 * @author  	create by allen.qiang
 */

quanli_require('module/base/common/quanliCommon.class.php');

class pa_goodsSelector{
	public function execute() {
		global $G_PGLOB ;
        if (isset($_REQUEST['operFlg'])) {
        	
            switch($_REQUEST['operFlg']) {

                case 1: //商品筛选器列表
                    $this->selectorList();
                break;
                case 2: //商品筛选器添加
                    $this->selectorAdd();
                break;
                case 3: //商品筛选器编辑
                    $this->selectorEdit();
                break;
                case 4: //商品筛选器详情查看
					$this->selectorView();
                break;    
				default:
					$G_PGLOB->smarty->assign('error_msg', '参数错误，请审核后再操作');
					$G_PGLOB->smarty->display('comm/error.tpl');
            }
        }
    }
	
	 /**
     * 商品筛选器列表
     * @author kangronghua
     * 
     */
    private function selectorList() {
        global $G_PGLOB;
        
        $G_PGLOB->smarty->display('promotion/goodsSelector_list.tpl');
    }
    
    /**
     * 筛选器添加 
     *
     */
    private function selectorAdd() {
    	global $G_PGLOB;
    	
    	$this->getFilter();
    }
    
    /**
     * 筛选器编辑 
     *
     */
    private function selectorEdit() {
    	global $G_PGLOB;
    	
    	//获取参数
    	$selectorID = isset($_REQUEST['selectorID']) ? trim($_REQUEST['selectorID']) : null;
    	if(empty($selectorID)){
    		$G_PGLOB->smarty->display('comm/error.tpl');
    		die();
    	}	
    		
    	//获取筛选器信息
    	$selectorInfo = new entity('goods_selector');
    	$selectorInfo->selectID = $selectorID;
    	if(!$G_PGLOB->DBCA->fetch($selectorInfo)){
    		$G_PGLOB->smarty->display('comm/error.tpl');
    		die();
    	}	
    		
    	//获取筛选规则信息
    	$selectorRule = $G_PGLOB->DBCA->stmt_query(sprintf('SELECT * FROM goods_selector_detail WHERE selectID = %d', $selectorID));
    	if($selectorRule){
    		foreach ($selectorRule as $k=>$rule){
    			$rule->ruleStr   = '';
    			$rule->ruleID    = '';
    			$rule->ruleType  = '';
    			switch ($rule->selectType){
    				case 1:	//分类
    					$ruleArr = explode(',', $rule->selector);
    					$rule->ruleType  = '分类';
    					if(count($ruleArr) == 1){	//大分类
    						$bigCategory = new entity('goods_bigcategory');
    						$bigCategory->bigCategoryID = $ruleArr[0];
    						if($G_PGLOB->DBCA->fetch($bigCategory)){
    							$rule->ruleStr   = $bigCategory->description;
    							$rule->ruleID    = 1 . '_' . $ruleArr[0];
    						}else 
    							unset($selectorRule[$k]);
    					}else if(count($ruleArr) == 2){	//中分类
    						$bigCategory = new entity('goods_bigcategory');
    						$bigCategory->bigCategoryID = $ruleArr[0];
    						if($G_PGLOB->DBCA->fetch($bigCategory)){
    							$midCategory = new entity('goods_midcategory');
    							$midCategory->bigCategoryID = $ruleArr[0];
    							$midCategory->midCategoryID = $ruleArr[1];
    							if($G_PGLOB->DBCA->fetch($midCategory)){
    								$rule->ruleStr   = $bigCategory->description . ' | ' . $midCategory->description;
    								$rule->ruleID    = 1 . '_' . $ruleArr[0] . '-' . $ruleArr[1];
    							}else 
    								unset($selectorRule[$k]);
    						}else 
    							unset($selectorRule[$k]);
    					}else if(count($ruleArr) == 3){	//小分类
    						$bigCategory = new entity('goods_bigcategory');
    						$bigCategory->bigCategoryID = $ruleArr[0];
    						if($G_PGLOB->DBCA->fetch($bigCategory)){
    							$midCategory = new entity('goods_midcategory');
    							$midCategory->bigCategoryID = $ruleArr[0];
    							$midCategory->midCategoryID = $ruleArr[1];
    							if($G_PGLOB->DBCA->fetch($midCategory)){
    								$smallCategory = new entity('goods_smallcategory');
    								$smallCategory->bigCategoryID   = $ruleArr[0];
    								$smallCategory->midCategoryID   = $ruleArr[1];
    								$smallCategory->smallCategoryID = $ruleArr[2];
    								if($G_PGLOB->DBCA->fetch($smallCategory)){
    									$rule->ruleStr   = $bigCategory->description . ' | ' . $midCategory->description . ' | ' . $smallCategory->description;
    									$rule->ruleID    = 1 . '_' . $ruleArr[0] . '-' . $ruleArr[1] . '-' . $ruleArr[2];
    								}else 
    									unset($selectorRule[$k]);
    							}else 
    								unset($selectorRule[$k]);
    						}else 
    							unset($selectorRule[$k]);
    					}
    				break;
    				case 2:	//货号
    					$rule->ruleStr   = $rule->selector;
		    			$rule->ruleID    = 3 . '_' . $rule->selector;
		    			$rule->ruleType  = '商家编码';
    				break;		
    				case 3:	//价格范围
    					$ruleArr = explode(',', $rule->selector);
    					$rule->ruleStr   = $rule->selector;
		    			$rule->ruleID    = 4 . '_' . $ruleArr[0] . $ruleArr[1];
		    			$rule->ruleType  = '价格范围';
    				break;	
    				case 4:	//品牌
    				
    					//查询品牌信息
                        /*$brandData = new entity('goods_brand');
                        $brandData->brandID = $rule->selector;
                        if($G_PGLOB->DBCA->fetch($brandData)){
                            $rule->ruleStr   = $brandData->brandName;
                            $rule->ruleID    = 2 . '_' . $rule->selector;
                            $rule->ruleType  = '品牌';
                        }*/

                        //edit by wanglulu 20141203 start
    					$brandData = new entity('sys_item');
    					$brandData->itemID = $rule->selector;
    					if($G_PGLOB->DBCA->fetch($brandData)){
    							$rule->ruleStr   = $brandData->itemName;
				    			$rule->ruleID    = 2 . '_' . $rule->selector;
				    			$rule->ruleType  = '品牌';
    					}
                        //edit by wanglulu 20141203 end
                        break;
    				case 5: //标签
    					$ruleArr = explode(',', $rule->selector);
    					$rule->ruleType  = '标签';
    					if(count($ruleArr) == 1){	//标签组
    						$tagGroup = new entity('goods_tag_group');
    						$tagGroup->tagGroupID = $ruleArr[0];
    						if($G_PGLOB->DBCA->fetch($tagGroup)){
    							$rule->ruleStr   = $tagGroup->tagGroupName;
				    			$rule->ruleID    = 5 . '_' . $ruleArr[0];
    						}
    					}else if(count($ruleArr) == 2){	//标签
    						$tagGroup = new entity('goods_tag_group');
    						$tagGroup->tagGroupID = $ruleArr[0];
    						if($G_PGLOB->DBCA->fetch($tagGroup)){
    							$tag = new entity('goods_tag');
    							$tag->tagGroupID = $ruleArr[0];
    							$tag->tagID      = $ruleArr[1];
    							if($G_PGLOB->DBCA->fetch($tag)){
    								$rule->ruleStr   = $tagGroup->tagGroupName . '|' . $tag->tagName;
					    			$rule->ruleID    = 5 . '_' . $ruleArr[0] . '-' . $ruleArr[1];
    							}
    						}
    					}
    				break;
    				case 6: //关键字
    					$key = 1000;
    					$rule->ruleStr   = $rule->selector;
		    			$rule->ruleID    = 'k' . $key;
		    			$rule->ruleType  = '关键字';
		    			
		    			$key++;
    				break;	
    			}
    		}
    	}

    	$G_PGLOB->smarty->assign('selectorInfo', $selectorInfo);
    	$G_PGLOB->smarty->assign('selectorRule', $selectorRule);
    	$this->getFilter();
    } 
    
    /**
     * 获取分类、品牌、标签组等信息
     *
     */
    public function getFilter(){
    	global $G_PGLOB;
    	
    	//获得大分类信息
    	$bigArray = $G_PGLOB->DBCA->stmt_query('SELECT bigCategoryID,description FROM goods_bigcategory WHERE useFlg = 1');
    	
    	//获得商品品牌信息
        //$goodsBrand = $G_PGLOB->DBCA->stmt_query('SELECT brandID,brandName FROM goods_brand WHERE useFlg = 1');

        //edit by wanglulu 20141203 start
    	$goodsBrand = $G_PGLOB->DBCA->stmt_query('SELECT itemID,itemName FROM sys_item WHERE itemCategoryID = 13 AND useFlg = 1');
        //edit by wanglulu 20141203 end

        //标签组
    	$tagGroupInfo = $G_PGLOB->DBCA->stmt_query('SELECT tagGroupID,tagGroupName,memoName FROM goods_tag_group WHERE useFlg = 1');
    	
    	$G_PGLOB->smarty->assign_by_ref('bigArray', $bigArray);
    	$G_PGLOB->smarty->assign_by_ref('goodsBrand', $goodsBrand);
    	$G_PGLOB->smarty->assign_by_ref('tagGroupInfo', $tagGroupInfo);
    	$G_PGLOB->smarty->display('promotion/goodsSelector_edit.tpl');
    }
    
    /**
     * 商品筛选器详情查看
     *
     */
    public function selectorView(){
    	global $G_PGLOB;

    	//获取参数
    	$selectID = isset($_REQUEST['selectID']) ? trim($_REQUEST['selectID']) : null;
    	if(empty($selectID)){
    		$G_PGLOB->smarty->display('comm/error.tpl');
    		die();
    	}
    	
    	$commHandler = new commHandler($G_PGLOB->DBCA);
		
		$arg_array = array('selectID' => $selectID);
		
		$result_detail = $commHandler->search($arg_array, 'goods_selector_detail');
		if($result_detail){
			foreach ($result_detail as $detail){
				switch ($detail->selectType){
            		case 1:
            			$detail->typeText     = '分类';
            			$detail->selectorText = '';
            			
            			//获取分类名称
            			$categoryArr = explode(',', $detail->selector);
            			if(count($categoryArr) == 1){ //大分类
            				$bigCategory = new entity('goods_bigcategory');
            				$bigCategory->bigCategoryID = $categoryArr[0];
            				if($G_PGLOB->DBCA->fetch($bigCategory))
            					$detail->selectorText = $bigCategory->description;
            			}else if(count($categoryArr) == 2){ //中分类
            				$bigCategory = new entity('goods_bigcategory');
            				$bigCategory->bigCategoryID = $categoryArr[0];
            				if($G_PGLOB->DBCA->fetch($bigCategory)){
	            				$midCategory = new entity('goods_midcategory');
	            				$midCategory->bigCategoryID = $categoryArr[0];
	            				$midCategory->midCategoryID = $categoryArr[1];
	            				if($G_PGLOB->DBCA->fetch($midCategory))
	            					$detail->selectorText = $bigCategory->description . ' | ' . $midCategory->description;
            				}		
            				
            			}else if(count($categoryArr) == 3){ //小分类
            				$bigCategory = new entity('goods_bigcategory');
            				$bigCategory->bigCategoryID = $categoryArr[0];
            				if($G_PGLOB->DBCA->fetch($bigCategory)){
	            				$midCategory = new entity('goods_midcategory');
	            				$midCategory->bigCategoryID = $categoryArr[0];
	            				$midCategory->midCategoryID = $categoryArr[1];
	            				if($G_PGLOB->DBCA->fetch($midCategory)){
		            				$smallCategory = new entity('goods_smallcategory');
		            				$smallCategory->bigCategoryID   = $categoryArr[0];
		            				$smallCategory->midCategoryID   = $categoryArr[1];
		            				$smallCategory->smallCategoryID = $categoryArr[2];
		            				if($G_PGLOB->DBCA->fetch($smallCategory))
		            					$detail->selectorText = $bigCategory->description . ' | ' . $midCategory->description . ' | ' . $smallCategory->description;
	            				}	
            				}		
            			}
            		break;	
            		case 2:
            			$detail->typeText = '货号';	
            			$detail->selectorText = $detail->selector;
            		break;
            		case 3:
            			$detail->typeText = '价格范围';	
            			$detail->selectorText = $detail->selector;
            		break;
            		case 4:
            			$detail->typeText = '品牌';	
            			$sys_item = new entity('sys_item');
                        $sys_item->itemID = $detail->selector;
            			if($G_PGLOB->DBCA->fetch($sys_item))
            				$detail->selectorText = $sys_item->itemName;

                        /*$brand = new entity('goods_brand');
                        $brand->brandID = $detail->selector;
                        if($G_PGLOB->DBCA->fetch($brand))
                            $detail->selectorText = $brand->brandName;
                        */
            		break;
            		case 5:
            			$detail->typeText = '标签';
            			$tagArr = explode(',', $detail->selector);
            			if(count($tagArr) == 1){	//标签组
            				$tagGroup = new entity('goods_tag_group');
            				$tagGroup->tagGroupID = $tagArr[0];
            				if($G_PGLOB->DBCA->fetch($tagGroup))
            					$detail->selectorText = $tagGroup->tagGroupName;
            					
            			}else if(count($tagArr) == 2){	//标签
            				$tagGroup = new entity('goods_tag_group');
            				$tagGroup->tagGroupID = $tagArr[0];
            				if($G_PGLOB->DBCA->fetch($tagGroup)){
	            				$tag = new entity('goods_tag');
	            				$tag->tagGroupID = $tagArr[0];
	            				$tag->tagID = $tagArr[1];
	            				if($G_PGLOB->DBCA->fetch($tag))
	            					$detail->selectorText = $tagGroup->tagGroupName . ' | ' . $tag->tagName;
            				}	
            			}
            		break;
            		case 6:
            			$detail->typeText = '关键词';	
            			$detail->selectorText = $detail->selector;
            		break;
            	}
			}
		}
		$G_PGLOB->smarty->assign_by_ref('result_detail', $result_detail);
    	$G_PGLOB->smarty->display('promotion/goodsSelector_view.tpl');
    }
}
?>